﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using HouseAgencySite.ViewModel;

namespace HouseAgencySite.Models
{
    public class Entity
    {

        [Display(Name = "创建时间")]
        [Editable(false)]
        public DateTime CreateTime { get; set; }

        [StringLength(36)]
        [Display(Name = "创建人ID")]
        [Editable(false)]
        public string CreateUserID { get; set; }
        [StringLength(36)]
        [Editable(false)]
        [Display(Name = "创建人")]

        public string CreateUserName { get; set; }
        [Display(Name = "修改时间")]
        public DateTime ModifyTime { get; set; }

        [StringLength(36)]
        [Display(Name = "修改人ID")]
        public string ModifyUserID { get; set; }

        [StringLength(36)]
        [Display(Name = "修改人")]
        public string ModifyUserName { get; set; }
        [Display(Name = "是否删除")]
        public bool IsDelete { get; set; }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public Entity Create(Entity entity,LogUserInfo userInfo) {
            
            entity.CreateTime = DateTime.Now;
            entity.CreateUserID = userInfo.Guid;
            entity.CreateUserName = userInfo.LogonName;

            entity.ModifyTime = DateTime.Now;
            entity.ModifyUserID = userInfo.Guid;
            entity.ModifyUserName = userInfo.LogonName;

            entity.IsDelete = false;
            return entity;
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public Entity Edit(AgencyDBContext db, Entity entity, LogUserInfo userInfo)
        {
            entity.ModifyTime = DateTime.Now;
            entity.ModifyUserID = userInfo.Guid;
            entity.ModifyUserName = userInfo.LogonName;
            db.Entry(entity).Property("CreateTime").IsModified = false;
            db.Entry(entity).Property("CreateUserID").IsModified = false;
            db.Entry(entity).Property("CreateUserName").IsModified = false;
            entity.IsDelete = false;
            return entity;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public Entity Delete(AgencyDBContext db, Entity entity, LogUserInfo userInfo)
        {
            entity.ModifyTime = DateTime.Now;
            entity.CreateUserID = userInfo.Guid;
            entity.CreateUserName = userInfo.LogonName;
            db.Entry(entity).Property("CreateTime").IsModified = false;
            db.Entry(entity).Property("CreateUserID").IsModified = false;
            db.Entry(entity).Property("CreateUserName").IsModified = false;
            entity.IsDelete = true;
            return entity;
        }
    }
}